package com.lishe.lishe.lishe.module.excitation.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.FlexibleRoundeBitmapDisplayer;
import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.MineExcitationObject;
import com.lishe.lishe.lishe.module.excitation.interfaces.OnPersonGiftCostListener;
import com.lishe.lishe.lishe.utils.Utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by DELL on 2017/10/26.
 */

public class PersonGiftListAdapter extends RecyclerviewBaseAdapter {

    private Context mContext;
    private List<MineExcitationObject> mList;
    private Map<String, MineExcitationObject> mMap = new HashMap<>();
    private OnPersonGiftCostListener mListener;
    private StringBuilder giftIds;
    private ImageLoaderUtils imageLoaderUtils;
    private boolean isExchange = false;//是否操作兑换
    private int mDisplayWidth;

    public PersonGiftListAdapter(Context context, List<MineExcitationObject> list, int displayWidth) {
        this.mContext = context;
        this.mList = list;
        this.mDisplayWidth = displayWidth;
        imageLoaderUtils = new ImageLoaderUtils(Utils.dip2px(mContext, 5), FlexibleRoundeBitmapDisplayer.CORNER_TOP_LEFT_RIGHT);
    }

    @Override
    public MyViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_person_gift, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
        if(payloads.isEmpty()){
            onBindViewHolder(holder,position);
        }else{
            try {
                checkVisible(holder,position);
            } catch (Exception e) {
                e.printStackTrace();
                onBindViewHolder(holder, position);
            }
        }
    }

    @Override
    public void onBindViewHolderMy(final RecyclerView.ViewHolder holder, int position) {
        final MineExcitationObject object = mList.get(position);
        imageLoaderUtils.displayImage(object.getGiftImgUrl(), ((MyViewHolder) holder).img);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isExchange) {
                    if (mMap.containsKey(object.getGiftId())) {
                        mMap.remove(object.getGiftId());
                        ((MyViewHolder) holder).check.setImageBitmap(null);
                    } else {
                        mMap.put(object.getGiftId(), object);
                        ((MyViewHolder) holder).check.setImageResource(R.mipmap.check);
                    }

                    double giftCost = getCheckGiftCost();
                    mListener.calculateGiftCost(giftCost, giftIds.toString(), mMap);
                }
            }
        });

        try {
            checkVisible(holder,position);
        } catch (Exception e) {
            e.printStackTrace();
        }

        ((MyViewHolder) holder).name.setText(object.getGiftName());
        ((MyViewHolder) holder).num.setText("x" + object.getNum());
        ((MyViewHolder) holder).giftCost.setText(object.getGiftCost() + "积分");
    }

    private void checkVisible(RecyclerView.ViewHolder holder,int position) throws Exception{
        if (isExchange) {
            ((MyViewHolder) holder).check.setVisibility(View.VISIBLE);
            if (mMap.containsKey(mList.get(position).getGiftId())) {
                ((MyViewHolder) holder).check.setImageResource(R.mipmap.check);
            } else {
                ((MyViewHolder) holder).check.setImageBitmap(null);
            }
        } else {
            ((MyViewHolder) holder).check.setVisibility(View.GONE);
        }
    }

    private double getCheckGiftCost() {
        giftIds = new StringBuilder();
        double giftCost = 0;
        Iterator<Map.Entry<String, MineExcitationObject>> it = mMap.entrySet().iterator();
        while (it.hasNext()) {
            MineExcitationObject mine = it.next().getValue();
            giftIds.append(mine.getGiftId() + ",");
            giftCost += mine.getGiftCost() * mine.getNum();
        }
        return giftCost;
    }

    @Override
    public int getItemCountMy() {
        return mList.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView img, check;
        private TextView name, num, giftCost;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            int width = (mDisplayWidth - Utils.dip2px(mContext, 10) * 4) / 3;
            img.setLayoutParams(new RelativeLayout.LayoutParams(width, width));
            name = itemView.findViewById(R.id.name);
            check = itemView.findViewById(R.id.check);
            num = itemView.findViewById(R.id.num);
            giftCost = itemView.findViewById(R.id.gift_cost);
        }
    }

    public void registerOnPersonGiftCostListener(OnPersonGiftCostListener listener) {
        this.mListener = listener;
    }

    public void unRegisterOnPersonGiftCostListener() {
        if (mListener != null) {
            mListener = null;
        }
    }

    public void exchangeSuccess() {
        mMap.clear();
    }

    /**
     * 是否进行兑换
     *
     * @param isExchange
     */
    public void setIsExchange(boolean isExchange) {
        this.isExchange = isExchange;
        notifyItemRangeChanged(0,mList.size(),"lishe");
    }

    public boolean getIsExchange() {
        return isExchange;
    }
}
